$(function() {
	//显示收货地址
	showArea();
	//$('#areaTable').datagrid('reload');
	/**   
	 * 增加收货地址
	 */
	$("#add_address_submit").click(function(){
		url=getRootPath()+"PersonCenterAction/addArea";
		var userPhone=$("#address_phone").val();
		var siteName=$("#address_address").val();
		$.ajax({
			async:false,
			Type:"POST",
			dataType:"JSON",
			data:{"userPhone":userPhone,"siteName":siteName},
			url:url,
			success:function(msg){
				if(msg!=null||msg!=""){
					alert("增加成功！")
					showArea();
				}
			},
			error:function(msg){
				alert("增加失败");
			}  
		});
		
		
	});
	
	
	/**
	 * 修改收货地址
	 */
	$("#update_submit").click(function(){
		
		url=getRootPath()+"PersonCenterAction/updateAreaTele";
		var userPhone=$("#address_phone").val();
		var siteName=$("#address_address").val();
		var userId=$("#user_id").val();
		var siteId=$("#site_id").val();
		$.ajax({
			async:false,
			Type:"POST",
			dataType:"JSON",
			data:{"siteName":siteName,"userPhone":userPhone,"userId":userId,"siteId":siteId},
			url:url,
			success:function(msg){
				if(msg!=null||msg!=""){
					alert("修改成功！");
					showArea();
				}
			},
			error:function(msg){
				alert("修改失败");
			}  
		});
	});
});


/**
 * 显示收货地址
 * @returns
 */
function showArea(){
	
	url=getRootPath()+"PersonCenterAction/findSite";
	var str=null;
	$.ajax({
		async:false,
		Type:"POST",
		dataType:"JSON",
		url:url,
		success:function(msg){
			for(var i=0; i<msg.length; i++){
				str+="<tr><td >"+msg[i].siteName+"</td><td >"+msg[i].userPhone+"</td>" +
						" <td ><a href='javascript:updateSeciverArea("+msg[i].siteId+")'>编辑</a> | <a href='javascript:deleteArea("+msg[i].siteId+")'>删除</a></td></tr>";
			
			}
			$("#showSite").html(str);
			
		},
		error:function(msg){
			alert("失败");
		}  
	});
}


/**
 * 删除的方法
 * @param id
 * @returns
 */
function deleteArea(id){
	var siteId=id;
	url=getRootPath()+"PersonCenterAction/deleteResiverArea";
	$.ajax({
		async:false,
		Type:"POST",
		dataType:"JSON",
		data:{"siteId":siteId},
		url:url,
		success:function(msg){
			if(msg!=null||msg!=""){
				alert("删除成功！");
				showArea();
			}
		},
		error:function(msg){
			alert("失败");
		}  
	});
	
}
/**
 * 显示需要修改的收货地址
 * @returns
 */
function updateSeciverArea(id){
	var siteId=id;
	url=getRootPath()+"PersonCenterAction/showUpdateArea";
	$.ajax({
		async:false,
		Type:"POST",
		dataType:"JSON",
		data:{"siteId":siteId},
		url:url,
		success:function(msg){
			for(var i=0;i<msg.length;i++){
				
				document.getElementById("site_id").value=siteId;
				document.getElementById("user_id").value=msg[i].userId;
				document.getElementById("address_address").value=msg[i].siteName;
				document.getElementById("address_phone").value=msg[i].userPhone;
			}
		},
		error:function(msg){
			alert("失败");
		}  
	});
}
